package com.whut.transportation.overweight;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.R.integer;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.whut.transportation.R;
import com.whut.transportation.adapter.OWinfoAdapter;
import com.whut.transportation.adapter.OWinfoAdapter.MyClickListener;
import com.whut.transportation.entity.AllWeighInfo;
import com.whut.transportation.entity.GuDao;
import com.whut.transportation.entity.Message;
import com.whut.transportation.entity.OverWeightInfo;
import com.whut.transportation.network.HttpManager;
import com.whut.transportation.network.HttpRequestVo;
import com.whut.transportation.parser.JSONParser;
import com.whut.transportation.ui.BaseActivity;
import com.whut.transportation.ui.BaseActivity.DataCallback;
import com.whut.transportation.utils.Constant;


public class OWTransInfoActivity extends BaseActivity implements MyClickListener{
	
	private ListView owinfo;
	private List<OverWeightInfo> overWeightInfos;
	private OWinfoAdapter owinfoAdapter;
	private GuDao gudao;
	private SharedPreferences preferences;
	
	private Button input; // 提交
	private Button allSelect; //全选
	private boolean checkSelect = true;
	
	private List<Integer> listItemID = new ArrayList<Integer>();
	//private SwipeRefreshLayout mSwipeLayout; 
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_add_more:
			getData(false);
			break;
		case R.id.input:
			minput();
			break;
		case R.id.allselect:
			if(checkSelect){
				checkSelect = false;
				selectAll();
				allSelect.setText("全不选");
			} else {
				checkSelect = true;
				noSelectAll();
				allSelect.setText("全选");
			}
			break;
		default:
			break;
		}
		super.onClick(v); 
	}
	
	private void minput() {
		listItemID.clear();
		for(int i = 0;i < owinfoAdapter.mcheck.size();i++){
			if (owinfoAdapter.mcheck.get(i)) {
				listItemID.add(i);
			}
		}
		if(listItemID.size()==0){
			Toast.makeText(mContext, "未选中任何条目无法执行批量检查", Toast.LENGTH_SHORT).show();
		} else {
			StringBuffer sBuffer = new StringBuffer("[");
			for (int i = 0; i < listItemID.size(); i++) {
				OverWeightInfo overWeightInfo = overWeightInfos.get(listItemID.get(i));
				sBuffer.append("{");
				sBuffer.append("danma:\"").append(overWeightInfo.getDanma()).append("\",");
				sBuffer.append("shunhao:\"").append(overWeightInfo.getShunNumber()).append("\"");
				sBuffer.append("},");
			}
			sBuffer.deleteCharAt(sBuffer.length()-1);
			sBuffer.append("]");
			//Toast.makeText(mContext,sBuffer.toString(), Toast.LENGTH_SHORT).show();
			HttpRequestVo vo = new HttpRequestVo();
			vo.requestUrl = Constant.M_SAVE_WEIGHDEALING_INFO;
			vo.context = mContext;
			vo.parser = new JSONParser(Message.class);
			HashMap<String, String> req = new HashMap<String, String>();
			req.put("params", sBuffer.toString());
			req.put("weighDealingInfo.gudao",gudao.getGudao());
			req.put("account", preferences.getString("account", null));
			vo.requestDataMap = req;
			getDataFromServer(HttpManager.POST_MOTHOD, vo, 
					new DataCallback<HashMap<String, Object>>(){
						@Override
						public void onStart() {
							
						}
						@Override
						public void onFailed() {
							showNetErrorDialog();
						}
						@Override
						public void processData(
								HashMap<String, Object> paramObject,
								boolean paramBoolean) {
							Intent intent = new Intent(OWTransInfoActivity.this,OWTransInfoActivity.class);
							intent.putExtra("gudao", gudao);
							startActivity(intent);
							Toast.makeText(mContext, "提交成功", Toast.LENGTH_SHORT).show();
							finish();
						}
						@Override
						public void onFinish() {
							closeProgressDialog();
						}
				
			});
		}
	}

	private void noSelectAll() {
		for (int i = 0; i < overWeightInfos.size(); i++) {
			owinfoAdapter.mcheck.set(i, false);
			owinfoAdapter.notifyDataSetChanged();
		}
	}

	private void selectAll() {
		for (int i = 0; i < overWeightInfos.size(); i++) {
			owinfoAdapter.mcheck.set(i, true);
			owinfoAdapter.notifyDataSetChanged();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (owinfoAdapter != null) {
			overWeightInfos.clear();
			owinfoAdapter.notifyDataSetChanged();
			getData(false);
		} else {
			getData(false);
		}
	}

	@Override
	protected void loadViewLayout() {
		setContentView(R.layout.activity_ow_transinfo);
		setTitleBarView(true, "首页", -1, true);
		preferences = getSharedPreferences("userId", Context.MODE_PRIVATE);
		Intent intent = getIntent();
		gudao = (GuDao) intent.getSerializableExtra("gudao");
	}
	@Override
	protected void findViewById() {
		owinfo = (ListView) findViewById(R.id.ow_info);
		input = (Button) findViewById(R.id.input);
		allSelect = (Button) findViewById(R.id.allselect);
		//mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);
	}

	@Override
	protected void afterFindViewId() {
		
	}

	@Override
	protected void processLogic() {
		overWeightInfos = new ArrayList<OverWeightInfo>();
	}

	@Override
	protected void setListener() {
		allSelect.setOnClickListener(this);
		input.setOnClickListener(this);
		/*owinfo.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
				if(overWeightInfos.size()>position){
					OverWeightInfo overWeightInfo = overWeightInfos.get(position);
					Intent intent = new Intent(OWTransInfoActivity.this,OverWeightDetailActivity.class);
					intent.putExtra("overWeightInfo", overWeightInfo);
					intent.putExtra("gudao", gudao);
					startActivity(intent);
				}
			}
		});*/
		
		/*mSwipeLayout.setOnRefreshListener(this);
		mSwipeLayout.setColorScheme(android.R.color.holo_blue_bright, android.R.color.holo_green_light,  
                android.R.color.holo_orange_light, android.R.color.holo_red_light);*/
	}

	@Override
	protected void getDataAgain() {
		
	}
	private void getData(final boolean isRefresh) {
		HttpRequestVo vo = new HttpRequestVo();
		vo.requestUrl = Constant.ALL_OVERWEIGH_INFO;
		vo.context = mContext;
		
		HashMap<String, String> req = new HashMap<String, String>();
		req.put("owstaffId", preferences.getString("userId", null));
		req.put("name", preferences.getString("name", null));
		req.put("gudao", String.valueOf(gudao.getGudao()));
		vo.requestDataMap = req;
		vo.parser = new JSONParser(AllWeighInfo.class);
		
		getDataFromServer(HttpManager.POST_MOTHOD, vo,new DataCallback<HashMap<String, Object>>() {
					@Override
					public void onStart() {
							showProgressDialog(getResources().getString(R.string.loading_content));
					}

					@Override
					public void onFailed() {
						showNetErrorDialog();
					}

					@Override
					public void processData(HashMap<String, Object> paramObject,boolean paramBoolean) {
						int code = (Integer)paramObject.get("code");
						if(code == 1){
							AllWeighInfo allWeighInfo = (AllWeighInfo) paramObject.get("result");
							if(isRefresh)
								overWeightInfos.clear();
								overWeightInfos.addAll(allWeighInfo.ListWeighInfo);
							if (owinfo.getAdapter()==null) {
								owinfoAdapter = new OWinfoAdapter(mContext, overWeightInfos, R.layout.activity_detail_owinfo_item,OWTransInfoActivity.this);
								owinfo.setAdapter(owinfoAdapter);
							} else {
								owinfoAdapter.notifyDataSetChanged();
							}
						} else {
							Toast.makeText(mContext, "目前数据为空", Toast.LENGTH_LONG).show();
						}
					}
					@Override
					public void onFinish() {
						closeProgressDialog();
					}
				});
	}

	@Override
	public boolean clickListener(View v, Integer tag) {
		Dandcl(tag);
		return false;
	}
	/**
	 * 单击单独处理按钮执行的操作
	 * 
	 */
	public void Dandcl(Integer tag){
		OverWeightInfo over = overWeightInfos.get(tag);
		HttpRequestVo vo = new HttpRequestVo();
		vo.requestUrl = Constant.SAVE_WEIGHDEALING_INFO;
		vo.context = mContext;
		vo.parser = new JSONParser(Message.class);
		HashMap<String, String> req = new HashMap<String, String>();
		req.put("account", preferences.getString("account", null));
		req.put("name", preferences.getString("name", null));
		req.put("id", over.getId());
		req.put("result", "0");
		req.put("weighDealingInfo.danma",over.getDanma());
		req.put("weighDealingInfo.shunhao",over.getShunNumber());
		req.put("weighDealingInfo.gudao",gudao.getGudao());
		vo.requestDataMap = req;
		getDataFromServer(HttpManager.POST_MOTHOD, vo, 
				new DataCallback<HashMap<String, Object>>() {
				@Override
				public void onStart() {
				}
				@Override
				public void onFailed() {
					showNetErrorDialog();
				}
				@Override
				public void processData(HashMap<String, Object> paramObject,boolean paramBoolean) {
					Intent intent = new Intent(OWTransInfoActivity.this,OWTransInfoActivity.class);
					intent.putExtra("gudao", gudao);
					startActivity(intent);
					Toast.makeText(mContext, "提交成功", Toast.LENGTH_SHORT).show();
					finish();
				}
				@Override
				public void onFinish() {
					closeProgressDialog();
				}
		});
		
	}

	/*@Override
	public void onRefresh() {
		getData(false);
		owinfoAdapter.notifyDataSetChanged();
	}*/
}
